package com.changan.controller;

import com.changan.error.BusinessException;
import com.changan.error.EmBusinessError;
import com.changan.response.CommonReturnType;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * 基类控制器,暂时我只写异常处理
 */
public class BaseController {
    @ExceptionHandler(Exception.class)
    public Object handlerException(Exception e){
        Map<String,Object> responseData = new HashMap<>();
        if(e instanceof BusinessException){
            //强转成BusinessException
            BusinessException businessException = (BusinessException)e;
            responseData.put("errCode",businessException.getErrCode());
            responseData.put("errMsg",businessException.getErrMsg());
        }else if(e instanceof ArithmeticException){
            responseData.put("errCode",EmBusinessError.ARITHMETIC_EXCEPTION.getErrCode());
            responseData.put("errMsg",EmBusinessError.ARITHMETIC_EXCEPTION.getErrMsg());
        }else if(e instanceof NullPointerException){
            responseData.put("errCode",EmBusinessError.NULL_POINTER_EXCEPTION.getErrCode());
            responseData.put("errMsg",EmBusinessError.NULL_POINTER_EXCEPTION.getErrMsg());
        }else{
            responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
            responseData.put("errMsg",EmBusinessError.UNKNOWN_ERROR.getErrMsg());
        }
        return CommonReturnType.create("fail",responseData);
    }
}
